﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KhachSan.Process.Public;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using System.Data.SqlClient;
namespace KhachSan.Process.Private.Bo
{
    public partial class UC_PhanQuyen : UserControl
    {
        public UC_PhanQuyen()
        {
            InitializeComponent();
        }
        int flag = -1;
        DataTable bang;
        private void bnThoat_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
     
        private void PhanQuyen_Load(object sender, EventArgs e)
        {
            LoadData();

            //List<string> list = bang.AsEnumerable().Select(r => r.Field<string>(0)).ToList();
          
            XLDuLieu.LoadCombobox("select MaNV,Ho+' '+Ten as HoTen From NhanVien", "HoTen", "MaNV",cbNhanVien );
            XLDuLieu.LoadCombobox("select MaNhom,TenNhom From NhomQuyen", "TenNhom", "MaNHom", cbNhom);
            if (bang.Rows.Count > 0)
            {
                luoi.FocusedRowHandle = 0;
                GanDuLieu();
            }
            else
            {
                bnSua.Enabled = false;
            }
        }

        private void LoadData()
        {
             bang = XLDuLieu.LoadDataTable("Select Ho,Ten,TenDangNhap,MatKhau,TenNhom,MatKhau2,KichHoat from TaiKhoanDangNhap join NhanVien on NhanVien.MaNV=TaiKhoanDangNhap.MaNV join NhomQuyen on NhomQuyen.MaNhom=TaiKhoanDangNhap.MaNhom");
            luoiDanhSachTaiKhoan.DataSource = bang;
        }

        private void bnThem_Click(object sender, EventArgs e)
        {
            txtTenDangNhap.Text = "";
            txtMatKhau.Text = "";
            cbNhom.Text = "";
            cbNhanVien.Text = "";
            cbKichHoat.Checked = true;
            txtMatKhauCap2.Text = "";
            EnableText(true);
            flag = 1;
            txtTenDangNhap.Focus();
            bnLuu.Enabled = true;
            bnSua.Enabled = false;
            bnUndo.Enabled = true;
            bnThem.Enabled = false;
            bnXoa.Enabled = false;
        }

        private void EnableText(bool value)
        {
            txtTenDangNhap.Enabled = value;
            txtMatKhau.Enabled = value;
            txtMatKhauCap2.Enabled = value;
            cbKichHoat.Enabled = value;
            cbNhom.Enabled = value;
            cbNhanVien.Enabled = value;

        }

        private void bnSua_Click(object sender, EventArgs e)
        {
            EnableText(true);
            flag = 2;
            txtTenDangNhap.Enabled = false;
            txtMatKhau.Focus();
            bnLuu.Enabled = true;
            bnThem.Enabled = false;
            bnUndo.Enabled = true;
            bnSua.Enabled = false;
            bnXoa.Enabled = false;
        }

        private void bnLuu_Click(object sender, EventArgs e)
        {
            string loi= KiemTra();
            if (loi == "")
            {
                if (flag == 1)
                    Them();
                else Sua();
                bnThem.Enabled = true;
                bnSua.Enabled = true;
                bnUndo.Enabled = false;
                bnLuu.Enabled = false;
            }
            else
            {
                MessageBox.Show(loi);
            }
           

           
        }

        private string KiemTra()
        {
            string loi = "";
            if (txtTenDangNhap.Text == "")
                loi += "Chưa nhập tên đăng nhập \r\n";
            if (txtMatKhau.Text == "")
                loi += "Chưa nhập mật khẩu\r\n";
            if (cbNhanVien.Text == "")
                loi += "Chưa chọn nhân viên \r\n";
            if (cbNhom.Text == "")
                loi += "Chưa chọn nhân viên \r\n";
            
            return loi;

        }
        private void Sua()
        {
            List<SqlParameter> list = new List<SqlParameter>();
            LayDuLieu(list);
            XLDuLieu.ExecuteSP("sp_SuaTaiKhoanDangNhap", list);
            LoadData();
            EnableText(false);
        }
        private void Them()
        {
            List<SqlParameter> list = new List<SqlParameter>();
            LayDuLieu(list);
            XLDuLieu.ExecuteSP("sp_ThemTaiKhoanDangNhap", list);
            LoadData();
            EnableText(false);
        }

        private void LayDuLieu(List<SqlParameter> list)
        {
            list.Add(new SqlParameter("@TenDangNhap", txtTenDangNhap.Text.Trim()));
            list.Add(new SqlParameter("@MatKhau", txtMatKhau.Text.Trim()));
            list.Add(new SqlParameter("@MaNV", cbNhanVien.SelectedValue.ToString().Trim()));
            list.Add(new SqlParameter("@MaNhom", cbNhom.SelectedValue.ToString().Trim()));
            list.Add(new SqlParameter("@MatKhau2", txtMatKhauCap2.Text.Trim()));
            list.Add(new SqlParameter("@KichHoat", cbKichHoat.Checked));
        }

        private void bnXoa_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn có thực sự muốn xóa ?", "Thông báo", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                int [] danhSach=   luoi.GetSelectedRows();
                for (int i = danhSach.Length-1;i>=0 ; i--)
                {
                    List<SqlParameter> list = new List<SqlParameter>();
                    list.Add(new SqlParameter("@TenDangNhap", bang.Rows[i]["TenDangNhap"]));
                    XLDuLieu.ExecuteSP("sp_XoaTaiKhoanDangNhap", list);
                }
                LoadData();
                bnLuu.Enabled = false;
            }
        }

        private void luoiDanhSachTaiKhoan_Click(object sender, EventArgs e)
        {
            GanDuLieu();
            int [] a= luoi.GetSelectedRows();
        }

        private void GanDuLieu()
        {
            try
            {
                DataRowView row = (DataRowView)luoi.GetFocusedRow();

                txtTenDangNhap.Text = row.Row["TenDangNhap"].ToString();
                txtMatKhau.Text = row.Row["MatKhau"].ToString();
                cbNhanVien.Text = row.Row["Ho"].ToString() + " " + row.Row["Ten"].ToString();
                cbNhom.Text = row.Row["TenNhom"].ToString();
                cbKichHoat.Checked = (bool)row.Row["KichHoat"];
                txtMatKhauCap2.Text = row.Row["MatKhau2"].ToString();
            }
            catch { }
        }

        private void bnUndo_Click(object sender, EventArgs e)
        {
            flag = 0;
            bnLuu.Enabled = false;
            bnUndo.Enabled = false;
            bnSua.Enabled = true;
            bnThem.Enabled = true;
            bnXoa.Enabled = true;
            GanDuLieu();
            EnableText(false);
        }

        private void txtTenDangNhap_Leave(object sender, EventArgs e)
        {
            if (flag == 1 && bnUndo.Focused==false)
            {
                DataRow[] r = bang.Select("TenDangNhap='" + txtTenDangNhap.Text + "'", "");
                if (r.Length > 0)
                {
                    txtTenDangNhap.Focus();
                    txtTenDangNhap.SelectionStart = 0;
                    txtTenDangNhap.SelectionLength = txtTenDangNhap.Text.Length;
                    MessageBox.Show("Tên đăng nhập này đã có");
                }
            }
        }

        private void txtTenDangNhap_KeyUp(object sender, KeyEventArgs e)
        {
           
        }

        private void txtTenDangNhap_KeyPress(object sender, KeyPressEventArgs e)
        {
            TestTextBox.OnlyCharAndNumber(e);
        }
    }
}
